﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NotificationClientTesting.NotificationServer;
using System.ServiceModel;
using System.Threading;

namespace NotificationClientTesting
{
    public partial class Form1 : Form
    {
        Device[] _devices;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;

            listView1.Columns.Add("id", 220);
            listView1.Columns.Add("name", 80);
            listView1.Columns.Add("device_type", 80);
            listView1.Columns.Add("registration_id", 280);
            listView1.Columns.Add("Sended to GCM", 120);
            listView1.Columns.Add("Recieved by Device", 120);
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {

            listView1.Items.Clear();

            try
            {


                ChannelFactory<IDeviceForMobile> cf;
                IDeviceForMobile proxy;
                cf = new ChannelFactory<IDeviceForMobile>("NetTcpBinding_IDeviceForMobile");
                proxy = cf.CreateChannel();
                _devices = proxy.GetDevices();
                ((IDisposable)proxy).Dispose();
                cf.Close();

                foreach (Device device in _devices)
                {
                    ListViewItem item = new ListViewItem(device.Id.ToString());
                    item.SubItems.Add(device.Name);
                    item.SubItems.Add(device.Device_type);
                    item.SubItems.Add(device.Registration_id);
                    item.SubItems.Add(String.Empty);
                    item.SubItems.Add(String.Empty);                    
                        
                    listView1.Items.Add(item);
                }

                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void InsertReturnMessage(string message)
        {
            richTextBox2.Clear();
            richTextBox2.Text = message;
        }

        private void btnSendNotification_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count < 1)
            {
                MessageBox.Show("Bitte Device auswählen");
                return;
            }

            if (richTextBox1.Text.Length < 1)
            {
                MessageBox.Show("Keine leere Notifications erlaubt");
                return;
            }

            Guid guid = new Guid(listView1.SelectedItems[0].Text);
            string notification = richTextBox1.Text;

            try
            {
                
                
                
                ChannelFactory<INotification> cf;
                INotification proxy;
                cf = new ChannelFactory<INotification>("NetTcpBinding_INotification");
                proxy = cf.CreateChannel();

                int index = listView1.SelectedItems[0].Index;
                proxy.SendNotification(guid, notification);
                


                /*if (proxy.SendNotification(guid, notification))
                {                   
                    listView1.Items[index].SubItems[4].ForeColor = Color.Green;
                    listView1.Items[index].SubItems[4].Text = "OK";
                }
                else
                {
                    listView1.Items[index].SubItems[4].ForeColor = Color.Red;
                    listView1.Items[index].SubItems[4].Text = "Fail";
                }*/
                    
                //((IDisposable)proxy).Dispose();
                cf.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}